IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[NotificationMode]') AND type in (N'U'))
CREATE TABLE [dbo].[NotificationMode](
	[NotificationModeId] [bigint] IDENTITY(1,1) NOT NULL,
	[ModeName] [varchar](50) NOT NULL,
	[NotificationInterval] [time](7) NULL,
 CONSTRAINT [PK_eftNotificationMode] PRIMARY KEY CLUSTERED 
(
	[NotificationModeId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO


IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[NotificationState]') AND type in (N'U'))
CREATE TABLE [dbo].[NotificationState](
	[NotificationStateId] [bigint] IDENTITY(1,1) NOT NULL,
	[StateName] [varchar](50) NOT NULL,
        [WorkflowName] [varchar](50) NOT NULL,
 CONSTRAINT [PK_eftNotificationState] PRIMARY KEY CLUSTERED 
(
	[NotificationStateId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO


IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[NotificationStateModes]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[NotificationStateModes](
	[NotificationStateModeId] [bigint] IDENTITY(1,1) NOT NULL,
	[UserName] [nvarchar](250) NOT NULL,
	[NotificationStateId] [bigint] NOT NULL,
	[NotificationModeId] [bigint] NOT NULL,
 CONSTRAINT [PK_NotificationStateModeSet] PRIMARY KEY CLUSTERED 
(
	[NotificationStateModeId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

ALTER TABLE [dbo].[NotificationStateModes]  WITH CHECK ADD  CONSTRAINT [FK_NotificationStateModeSet_NotificationMode] FOREIGN KEY([NotificationModeId])
REFERENCES [dbo].[NotificationMode] ([NotificationModeId])

ALTER TABLE [dbo].[NotificationStateModes] CHECK CONSTRAINT [FK_NotificationStateModeSet_NotificationMode]

ALTER TABLE [dbo].[NotificationStateModes]  WITH CHECK ADD  CONSTRAINT [FK_NotificationStateModeSet_NotificationState] FOREIGN KEY([NotificationStateId])
REFERENCES [dbo].[NotificationState] ([NotificationStateId])

ALTER TABLE [dbo].[NotificationStateModes] CHECK CONSTRAINT [FK_NotificationStateModeSet_NotificationState]
END
GO


IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[NotificationInstances]') AND type in (N'U'))
CREATE TABLE [dbo].[NotificationInstances](
	[UserNotificationID] [int] NOT NULL,
	[UserName] [nvarchar](500) NOT NULL,
	[WorkflowInstanceId] [int] NOT NULL,
	[WorkflowName] [nvarchar](200) NOT NULL,
	[StateName] [nvarchar](200) NOT NULL,
	[NotificationSubject] [ntext] NOT NULL,
	[NotificationBody] [ntext] NOT NULL,
	[NotificationSent] [bit] NOT NULL,
	[GenerationTime] [datetime] NOT NULL,
 CONSTRAINT [PK_NotificationInsances] PRIMARY KEY CLUSTERED 
(
	[UserNotificationID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

IF NOT EXISTS(select * from NotificationMode where NotificationInterval is NULL)
   INSERT INTO [dbo].[NotificationMode] ([ModeName],[NotificationInterval]) VALUES ('Each Transaction',NULL)
IF NOT EXISTS(select * from NotificationMode where NotificationInterval='08:00:00')
   INSERT INTO [dbo].[NotificationMode] ([ModeName],[NotificationInterval])VALUES ('Consolidated At 8 AM','08:00:00')
IF NOT EXISTS(select * from NotificationMode where NotificationInterval='12:00:00')
   INSERT INTO [dbo].[NotificationMode] ([ModeName],[NotificationInterval])VALUES ('Consolidated At 12 PM','12:00:00')
IF NOT EXISTS(select * from NotificationMode where NotificationInterval='16:00:00')
   INSERT INTO [dbo].[NotificationMode] ([ModeName],[NotificationInterval])VALUES ('Consolidated At 4 PM','16:00:00')
IF NOT EXISTS(select * from NotificationMode where NotificationInterval='20:00:00')
   INSERT INTO [dbo].[NotificationMode] ([ModeName],[NotificationInterval])VALUES ('Consolidated At 8 PM','20:00:00')

if not exists(select * from NotificationState where StateName='Awaiting Approval' and WorkflowName='UserWorkflow')
   INSERT INTO [dbo].[NotificationState] ([StateName],[WorkflowName])  VALUES ('Awaiting Approval','UserWorkflow')
if not exists(select * from NotificationState where StateName='Approved' and WorkflowName='UserWorkflow')
   INSERT INTO [dbo].[NotificationState] ([StateName],[WorkflowName])  VALUES ('Approved','UserWorkflow')
if not exists(select * from NotificationState where StateName='Rejected' and WorkflowName='UserWorkflow')
   INSERT INTO [dbo].[NotificationState] ([StateName],[WorkflowName])  VALUES ('Rejected','UserWorkflow')
if not exists(select * from NotificationState where StateName='Cancelled' and WorkflowName='UserWorkflow')
INSERT INTO [dbo].[NotificationState] ([StateName],[WorkflowName])  VALUES ('Cancelled','UserWorkflow')
if not exists(select * from NotificationState where StateName='Awaiting Approval' and WorkflowName='BankAccountWorkflow')
   INSERT INTO [dbo].[NotificationState] ([StateName],[WorkflowName])  VALUES ('Awaiting Approval','BankAccountWorkflow')
if not exists(select * from NotificationState where StateName='Approved' and WorkflowName='BankAccountWorkflow')
   INSERT INTO [dbo].[NotificationState] ([StateName],[WorkflowName])  VALUES ('Approved','BankAccountWorkflow')
if not exists(select * from NotificationState where StateName='Rejected' and WorkflowName='BankAccountWorkflow')
   INSERT INTO [dbo].[NotificationState] ([StateName],[WorkflowName])  VALUES ('Rejected','BankAccountWorkflow')
if not exists(select * from NotificationState where StateName='Cancelled' and WorkflowName='BankAccountWorkflow')
   INSERT INTO [dbo].[NotificationState] ([StateName],[WorkflowName])  VALUES ('Cancelled','BankAccountWorkflow')


